﻿using System.Collections.Generic;

namespace Wolfpack.Periscope.Core.Extensions
{
    public static class DictionaryExtensions
    {
        public static T AddFromAnonymous<T>(this T target, object anonymous)
            where T : IDictionary<string, string>
        {


            foreach (var propertyInfo in anonymous.GetType().GetProperties())
            {
                var value = propertyInfo.GetValue(anonymous, null);
                string setValue = null;

                if (value != null)
                    setValue = value.ToString();

                target.Add(propertyInfo.Name, setValue);
            }
            return target;
        }
        public static T RemoveFromAnonymous<T>(this T target, object anonymous)
            where T : IDictionary<string, string>
        {
            foreach (var propertyInfo in anonymous.GetType().GetProperties())
            {
                if (target.ContainsKey(propertyInfo.Name))
                    target.Remove(propertyInfo.Name);
            }

            return target;
        }
    }
}